/**
 * Copyright (c)  2012, William Ulrich
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <ORGANIZATION> nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <NN_Controller.hh>

using namespace std;

NN_Controller::NN_Controller(void)
{
  // Regular Spiking neurons
  std::string rs_name("RS");
  int num_rs_params = 7;
  float rs_params[] = { 0.02, 0.20, -61.3, 6.5, -13.0, -65.0, 1.0 };
  
  IzhikevichNeuronType * rs = new IzhikevichNeuronType(rs_name, num_rs_params, rs_params);
  rs->setWeights(0.3, 0.7);

  // Fast Spiking neurons
  std::string fs_name("FS");
  int num_fs_params = 7;
  float fs_params[] = { 0.02, 0.25, -65.0, 2, -13.0, -65.0, 0.0 };
  
  IzhikevichNeuronType * fs = new IzhikevichNeuronType(fs_name, num_fs_params, fs_params);
  fs->setWeights(-0.5, -0.4);
  
  lsm_ = new LSM(20,10,5);
  
  lsm_->addNeuronType(rs, 0.8);
  lsm_->addNeuronType(fs, 1.0);
  
  lsm_->initReservoir();

  std::string rf_topic("/std_cam_rf");
  addReceptiveField(rf_topic, 20, 10);

  // lsm_->finalize();

  // clock_sub_ = nh_.subscribe<rosgraph_msgs::Clock>("/clock", 10, &NN_Controller::step, this);

  // prev_nsec_ = 0;
}

NN_Controller::~NN_Controller(void)
{
  delete lsm_;
}

void NN_Controller::step(const rosgraph_msgs::Clock::ConstPtr& t)
{
  unsigned long curr_nsec = t->clock.nsec / 1000000;
  int span = curr_nsec - prev_nsec_;

  if(span < 0) 
    span = 1000 + span;

  std::cout << span << '\n';

  for(int i = 0; i < span; i++)
    {
      const vector<unsigned> fired = lsm_->step();
    }

  prev_nsec_ = curr_nsec;

}

void NN_Controller::addReceptiveField(std::string RFTopic, int x, int y)
{
  // add to LSM
  lsm_->addInputLayer(RFTopic, x, y);

  // subscribe to associated topic
  rf_sub_ = nh_.subscribe<vision::RF>(RFTopic, 1, boost::bind(&NN_Controller::updateRF, this, _1, RFTopic));

}

void NN_Controller::updateRF(const ros::MessageEvent<vision::RF const>& event, 
			     const std::string& topic)
{
  const vision::RF::ConstPtr& msg = event.getMessage();
 
  std::vector<std::pair<int,float> > values;
  for (int i = 0; i < msg->rf.size(); i++)
    {
      values.push_back(std::pair<int,float>( msg->rf[i].index, msg->rf[i].val ));      
    } 

  lsm_->updateInputlayer(topic, values);
}

int main(int argc, char *argv[])
{
  ros::init(argc, argv, "nn_controller");
  NN_Controller nnc;

  ros::spin();    
}
